/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Base for creating a custom VGUI class; remove the functions you don't use.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
include( "shared.lua" )
require( "datastream" )


local PANEL = {}
local ShowStatus = true
local SelLine = -1

function PANEL:Init()
	//Create controls here
	local SPanel = self
	
	self:SetTitle( "Notices" )
	self:ShowCloseButton( true )
	ShowStatus = true
	
	RunConsoleCommand( "StreamNotices" )
	
	
	local NoticeList = vgui.Create( "DListView", self )
		local NNum = NoticeList:AddColumn( "##" )
		local NText = NoticeList:AddColumn( "Notice" )
		NNum:SetMinWidth( 20 )
		NNum:SetMaxWidth( 20 )
		NoticeList:SetMultiSelect( false )
		local OCL = NoticeList.OnClickLine
		NoticeList.OnClickLine = function(parent, line, isselected)
			SelLine = tonumber( line:GetValue(1) )
			
			OCL(parent, line, isselected)
		end
		self.NoticeList = NoticeList
		
		
	local AddButton = vgui.Create( "DButton", self )
		AddButton:SetText( "Add Notice" )
		function AddButton:DoClick()
			SPanel:DoAddNotice()
		end
		self.AddButton = AddButton
	
	
	local EditButton = vgui.Create( "DButton", self )
		EditButton:SetText( "Edit Notice" )
		function EditButton:DoClick()
			if SelLine > 0 then
				SPanel:DoAddNotice( ReadNotices[SelLine] )
			end
		end
		self.EditButton = EditButton
		
		
	local RemoveButton = vgui.Create( "DButton", self )
		RemoveButton:SetText( "Remove Notice" )
		function RemoveButton:DoClick()
			if SelLine > 0 then
				table.remove( ReadNotices, SelLine )
				datastream.StreamToServer( "SaveNoticesStream", { ReadNotices } )
				SPanel.NoticeList:Clear()
				SPanel:AddNotices()
			end
		end
		self.RemoveButton = RemoveButton
		
		
	local SStatus = vgui.Create( "DLabel", self )
		SStatus:SetText( "Receiving Notices..." )
		SStatus:NoClipping( true )
		SStatus:SizeToContents()
		self.SStatus = SStatus
	
	
	self:SetSize( 350, 480 )
	self:MakePopup()
	self:Center()

end

function PANEL:DoAddNotice( NOTICE )	//Work on disabling input on the frames behind this kind of stuff
	local SPanel = self
	local TEXT = "Add your notice here."
	if NOTICE then 
		TEXT = NOTICE
	end

	local AddFrame = vgui.Create( "DFrame" )
		AddFrame:SetTitle( "Add Notice" )
		AddFrame:SetSize( 300, 100 )
		AddFrame:Center()
		AddFrame:MakePopup()
		
	local AddEntry = vgui.Create( "DTextEntry", AddFrame )
		AddEntry:StretchToParent( 2, 2, 2, AddFrame:GetTall() * 0.6 )
		AddEntry:SetValue( TEXT )
		
	local OK = vgui.Create( "DButton", AddFrame )
		OK:SetText( "OK" )
		OK:SetSize( 70, 30 )
		OK:AlignBottom( 5 )
		OK:AlignLeft( 5 )
		function OK:DoClick()
			table.insert( ReadNotices, AddEntry:GetValue() )
			if NOTICE then
				table.remove( ReadNotices, SelLine )
			end
			datastream.StreamToServer( "SaveNoticesStream", { ReadNotices } )
			SPanel.NoticeList:Clear()
			SPanel:AddNotices()
			AddFrame:Remove()
		end
	
	local CANCEL = vgui.Create( "DButton", AddFrame )
		CANCEL:SetText( "Cancel" )
		CANCEL:SetSize( 70, 30 )
		CANCEL:AlignBottom( 5 )
		CANCEL:AlignRight( 5 )
		function CANCEL:DoClick()
			AddFrame:Remove()
		end
end

function PANEL:AddNotices()
	
	for k,v in pairs( ReadNotices ) do
		self.NoticeList:AddLine( k, v )
	end

end

function PANEL:PerformLayout()	//350 wide
	// Let's make sure we perform the layout of the class this panel is based on here as well.
	self.BaseClass.PerformLayout( self )
	
	self.NoticeList:StretchToParent( 2, 20, 2, self:GetTall() * 0.3 )
	
	self.AddButton:SetSize( 100, 30 )
	self.AddButton:AlignLeft( 5 )
	self.AddButton:AlignBottom( 10 )
	
	self.EditButton:SetSize( 100, 30 )
	self.EditButton:Center()
	self.EditButton:AlignBottom( 10 )
	
	self.RemoveButton:SetSize( 100, 30 )
	self.RemoveButton:AlignRight( 5 )
	self.RemoveButton:AlignBottom( 10 )
	
	self.SStatus:Center()

end

function PANEL:Paint()
	//Do Painting here
	self.BaseClass.Paint( self )
	
	surface.SetDrawColor( Color( 50, 50, 50, 255 ) )
	surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	
	surface.SetDrawColor( Color( 20, 20, 20, 255 ) )
	surface.DrawOutlinedRect( 0, 0, self:GetWide(), self:GetTall() )

end

function PANEL:PaintOver()
	//Paint over self and child elements here.
	
	if ShowStatus then
		surface.SetDrawColor( Color( 255, 255, 255, 75 ) )
		surface.DrawRect( 0, 0, self:GetWide(), self:GetTall() )
	end

end

function PANEL:Think()
	//Think stuff here
	
	if LocalPlayer():GetNWInt( "nstream" ) == 1 then
		self:AddNotices()
		LocalPlayer():SetNWInt( "nstream", 0 )
		self.SStatus:SetVisible( false )
		ShowStatus = false
	end

end

function PANEL:OnCursorEntered()

	//Do stuff when the cursor enters the panel

end

function PANEL:OnCursorExited()

	//Do Stuff when the cursor leaves the panel

end

function PANEL:OnMouseWheeled()

	//Do stuff when you wheel the mouse on this panel

end

function PANEL:OnMousePressed()

	//Do stuff when you press the mouse on this panel

end

function PANEL:OnMouseReleased()

	//Do stuff when you release the mouse on this panel

end

function PANEL:OnKeyCodePressed()

	//Do stuff when using keys from the keyboard on this panel. This hook uses the keycode enumerations
	//http://wiki.garrysmod.com/?title=P.OnKeyCodePressed	For more information

end

function PANEL:ActionSignal()

	//This is called on the parent frame when a child VGUI element is used. The key corresponds to what is set by Panel.SetCommand.
	//From the wiki
	
end

function PANEL:OnCursorMoved()

	//From the wiki
	//Called whenever the the mouse is moved within the panel.

end

function PANEL:DoClick()

	//For button type panels, use this. Called when you click the button.

end


//Register syntax - Desired element name, Panel to use, base class of the new element.
vgui.Register( "NoticeEditor", PANEL, "DFrame" )